package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.dto.SetmealDTO;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.mapper.SetMealMapper;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetMealService;
import com.itheima.health.utils.aliyunoss.AliyunUtils;
import com.itheima.health.utils.redis.RedisUtil;
import com.itheima.health.utils.resources.RedisConstant;
import com.itheima.health.vo.CheckGroupVO;
import com.itheima.health.vo.SetmealVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

@Service
@Transactional
public class SetMealServiceImpl extends ServiceImpl<SetMealMapper,Setmeal> implements SetMealService {


    @Override
    public Boolean saveSetMeal(SetmealDTO setmealDTO) {

        boolean flag = saveOrUpdate(setmealDTO);
        Integer id = setmealDTO.getId();
        //  中间表 t_setmeal_checkgroup  录入数据
        Integer[] checkgroupIds = setmealDTO.getCheckgroupIds();
        for (Integer checkgroupId : checkgroupIds) {
            baseMapper.addSetmealAndGroupInfo(id,checkgroupId);
        }
        //  set集合移除图片信息
        RedisUtil.removeSetMember(RedisConstant.ALL_SETMEAL_PIC_SET,setmealDTO.getImg());
        return flag;  //
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //  条件构造出来   Wrapper对象
        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<Setmeal>();
        Page<Setmeal> page = null;
        if(StringUtils.isBlank(queryPageBean.getQueryString())){
            page = page(new Page<Setmeal>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()));
        }else{
            queryWrapper.like("HELPCODE",queryPageBean.getQueryString());
            //   sql   + 追加  sql  where  name like  %?%
            page = page(new Page<Setmeal>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()),queryWrapper);
        }
        PageResult  result = new PageResult(page.getTotal(),page.getRecords());
        return result;
    }

    @Override
    public void clearOssImg() {
        Set<String> members = RedisUtil.getMembersOfSet(RedisConstant.ALL_SETMEAL_PIC_SET);
        if(members.size()!=0){
            for (String uuidfilename : members) {
                //   遍历 图片  判断 redis 该图片有没有存在
                boolean existsKey = RedisUtil.existsKey(RedisConstant.SINGLE_PIC + uuidfilename);
                if(existsKey==false){
                    //   set集合中没找到  redis 对应得图片信息  那么通知 oss删除图片即可
                    AliyunUtils.deleteFile(uuidfilename);
                    System.out.println("-----阿里云删除垃圾图片一次----");
                    //  set集合  垃圾图片 删除
                    RedisUtil.removeSetMember(RedisConstant.ALL_SETMEAL_PIC_SET,uuidfilename);
                }
            }
        }

    }

    @Override
    public SetmealVO findSetMealDetail(int id) {
        //5表sql查询拆分
        //根据套餐id查询套餐
        SetmealVO setmealVO = baseMapper.findSetmealById(id);

        //根据套餐id查询检查组
        List<CheckGroupVO> checkGroupVOList = baseMapper.findCheckgroupBySetmealId(id);

        //根据检查组id查询检查项
        for (CheckGroupVO checkGroupVO : checkGroupVOList) {
            List<CheckItem> checkItemList = baseMapper.findCheckitemByCheckgroupId(checkGroupVO.getId());
            checkGroupVO.setCheckItemList(checkItemList);
        }
        setmealVO.setCheckGroupList(checkGroupVOList);
        return setmealVO;
    }


}
